#include "main_menu.hpp"

#include "game_menu.hpp"
#include "options_menu.hpp"

#include <iostream>

MainMenu::MainMenu() {

}

MainMenu::~MainMenu() {

}

void MainMenu::init(Engine *engine) {

    selectedMenuItem = 0;

	sf::String games;
	games.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	games.SetText("Game");
	games.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 260));
	games.SetCenter(sf::Vector2f(games.GetRect().GetWidth() / 2.f, games.GetRect().GetHeight() / 2.f));
	games.SetColor(sf::Color(255, 255, 255, 255));
	games.SetScale(sf::Vector2f(1.2f, 1.2f));

	sf::String options;
	options.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	options.SetText("Options");
	options.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 300));
	options.SetCenter(sf::Vector2f(options.GetRect().GetWidth() / 2.f, options.GetRect().GetHeight() / 2.f));
	options.SetColor(sf::Color(255, 255, 255, 255));

    sf::String quit;
	quit.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	quit.SetText("Quit");
	quit.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 360));
	quit.SetCenter(sf::Vector2f(quit.GetRect().GetWidth() / 2.f, quit.GetRect().GetHeight() / 2.f));
	quit.SetColor(sf::Color(255, 255, 255, 255));

	menuItems.push_back(games);
	menuItems.push_back(options);
	menuItems.push_back(quit);

	background.SetImage(engine->getImage("data/images/main-menu.png"));

	/*themeMusic.Open("data/music/Goof.ogg"); //STFU
	themeMusic.Play();*/
}

void MainMenu::cleanup() {
	//themeMusic.Stop();
	menuItems.clear();
}

void MainMenu::pause() {
	//themeMusic.Stop();
}

void MainMenu::resume() {
    //themeMusic.Play();
}

void MainMenu::processEvent(Engine *engine, PlayerEvent& event) {
	switch(event.type) {
		case PlayerEvent::MoveUp:
		{
			if(event.state)
				if(selectedMenuItem > 0) {
					menuItems[selectedMenuItem].SetScale(sf::Vector2f(1.f, 1.f));
					selectedMenuItem--;
					menuItems[selectedMenuItem].SetScale(sf::Vector2f(1.2f, 1.2f));
				}
			break;
		}
		case PlayerEvent::MoveDown:
		{
			if(event.state)
				if(selectedMenuItem < menuItems.size() - 1) {
					menuItems[selectedMenuItem].SetScale(sf::Vector2f(1.f, 1.f));
					selectedMenuItem++;
					menuItems[selectedMenuItem].SetScale(sf::Vector2f(1.2f, 1.2f));
				}
			break;
		}
		case PlayerEvent::Action1:
		{
			if(event.state) {
				switch(selectedMenuItem) {
					case 0:
						engine->pushState(GameMenu::getSingleton());
						break;
					case 1:
						engine->pushState(OptionsMenu::getSingleton());
						break;
					default:
						engine->quit();
						break;
				}
			}
			break;
		}
		case PlayerEvent::Quit:
		{
				if(event.state)
					engine->quit();
			break;
		}
		default:
			break;
	}
}

void MainMenu::update(Engine *engine) {
	/*if(gralaby.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY())) gralaby.SetScale(sf::Vector2f(1.2f, 1.2f));
	else gralaby.SetScale(sf::Vector2f(1.0f, 1.0f));

	if(gunBuns.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY())) gunBuns.SetScale(sf::Vector2f(1.2f, 1.2f));
	else gunBuns.SetScale(sf::Vector2f(1.0f, 1.0f));

	if(quit.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY())) quit.SetScale(sf::Vector2f(1.2f, 1.2f));
	else quit.SetScale(sf::Vector2f(1.0f, 1.0f));*/
}

void MainMenu::draw(Engine *engine) {
	engine->window.Draw(background);

	for(std::vector<sf::String>::iterator iMenuItem = menuItems.begin(); iMenuItem != menuItems.end(); iMenuItem++)
		engine->window.Draw(*iMenuItem);
}
